//
//  MainViewController.swift
//  微博Swift
//
//  Created by  justinchou on 16/2/23.
//  Copyright © 2016年  justinchou. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    // MARK:- 监听方法
    @objc private func clickComposeButton() {
        print("撰写按钮")
        //TODO:-撰写界面实现
    }
    
    // MARK:- 生命周期
    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        setupSeperateLine()
        setupComposeBtn()
        
        showNewFeatureView()
    }
    
    // MARK:- 懒加载控件
    private lazy var composeButton: UIButton = {
        
        let btn = UIButton()
        
        let foreImageName = "tabbar_compose_icon_add"
        let backImageName = "tabbar_compose_button"
        
        btn.setImage(UIImage(named: foreImageName), forState: .Normal)
        btn.setImage(UIImage(named: foreImageName + "_highlighted"), forState: .Highlighted)
        
        btn.setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
        
        btn.sizeToFit()
        
        return btn
        
    }()
    
}

// MARK:- 欢迎和新特性界面
extension MainViewController {
    /// 显示新特性界面
    private func showNewFeatureView() {
        //1.判断是否登录,false直接返回
        if UserAccount.sharedUserAccount.isLogin == false {
            return
        }
        //登录之后，判断是否为新版本 
            //y 新特性界面 n欢迎界面
        let v = isNewVersion ? NewFeatureView() : WelcomeView()
        
        view.addSubview(v)
        v.frame = view.bounds
    
    }
    
    //计算型属性判断是否为新版本
    private var isNewVersion: Bool {
        
        //1 读取版本号
        let version = NSBundle.justin_currentversion
        
        //2 读取沙盒版本号
        let sandboxVersion = NSUserDefaults.standardUserDefaults().objectForKey(JustinWeiboVersionKey) as? String
        
        //3 将当前版本号添加到偏好设置中
        NSUserDefaults.standardUserDefaults().setObject(version, forKey: JustinWeiboVersionKey)
        
        //4 返回比较结果
        return version > sandboxVersion
    }
    
}

// MARK:- 设置TabBar的内容
extension MainViewController {
    /// 设置撰写按钮
    private func setupComposeBtn() {
        
        tabBar.addSubview(composeButton)
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        //将子控件放在父控件视图层次的最上层
        tabBar.bringSubviewToFront(composeButton)
        composeButton.addTarget(self, action: "clickComposeButton", forControlEvents: .TouchUpInside)
        composeButton.frame = CGRectInset(tabBar.bounds, 2 * width, 0)
    }
    /// 设置tabbar分隔线的样式
    private func setupSeperateLine() {
        //必须2个方法都写才能生效
        tabBar.shadowImage = UIImage.justion_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
    }
}

// MARK:- 设置子控制器们
extension MainViewController {

    /// 添加子控制器们
    private func addChildViewControllers() {
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    /// 添加子控制器
    ///
    /// - parameter vc:        控制器类型
    /// - parameter title:     标题
    /// - parameter imageName: 图片
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置图片必须要设置图片的渲染模式imageWithRenderingMode(.AlwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //设置tabBarItem的文本属性方法
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
    
}
